package net.zjitc.dao;


import net.zjitc.Utils.C3P0Utils;
import net.zjitc.domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * Created by hasee on 2017/9/26.
 */
public class UserDao {
    public User findByUsername(String username) throws SQLException {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from user where username = ?";
        User user = (User) runner.query(sql,new BeanHandler(User.class),username);
        return user;
    }

    public int login(User user) throws SQLException {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String username = user.getUsername();
        String password = user.getPassword();
        User u = findByUsername(username);
        if(u==null){return 1;}//错误代码1 用户名不存在
        if(!password.equals(u.getPassword())){return 2;}//错误代码2 密码错误
        return 3;//代码3 校验成功
    }

}
